/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package au.com.domlogic.pvc.xhtml.document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;

/**
 *
 * @author Domenic Figliomeni
 */
public class XHTMLDocumentFactory {

    private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY =
            DocumentBuilderFactory.newInstance();
    public static final String NAMESPACE = "http://www.w3.org/1999/xhtml";

    static {
        DOCUMENT_BUILDER_FACTORY.setNamespaceAware(true);
    }

    /**
     * Factory method for creating XHTML Strict 1.0 Documents.
     * 
     * @return the newly created {@link XHTMLStrictDocument}
     */
    public static XHTMLStrictDocument newXHTMLStrictDocument() {
        XHTMLStrictDocument xhtmlStrict = null;
        try {
            DocumentBuilder builder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
            DocumentType doctype = builder.getDOMImplementation().createDocumentType(
                    "html",
                    "-//W3C//DTD XHTML 1.0 Strict//EN",
                    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
            Document document =
                    builder.getDOMImplementation().createDocument(
                    NAMESPACE, "html", doctype);
            document.getDocumentElement().appendChild(
                    document.createElement("head")).appendChild(
                    document.createElement("title"));
            document.getDocumentElement().appendChild(
                    document.createElement("body"));

            xhtmlStrict = new XHTMLStrictDocument(document);
        } catch (ParserConfigurationException pce) {
            throw new RuntimeException(pce);
        }
        return xhtmlStrict;
    }
}
